import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_provider_demo/providers/WeatherInfo.dart';

class SingleProviderDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //通过Provider.of<>(context)来获取数据
    var weatherInfo = Provider.of<WeatherInfo>(context);
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('Single Provider Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            //通过Consumer获取数据
            Consumer<WeatherInfo>(builder: (context, weather, _) {
              return Text(
                weather.temperatureValue.toString(),
                style: TextStyle(fontSize: 30),
              );
            }),
            Text(
              weatherInfo.tempType,
              style: TextStyle(fontSize: 30),
            ),
            RaisedButton(
              onPressed: () {
                String newTemperatureType = weatherInfo.tempType == 'Celcius'
                    ? 'Fahrenheit'
                    : 'Celcius';
                weatherInfo.temperatureType = newTemperatureType;
              },
              child: Text('改变温度单位'),
            ),
            RaisedButton(
              onPressed: () {
                weatherInfo.temperatureValue++;
              },
              child: Text('温度+1'),
            )
          ],
        ),
      ),
    );
  }
}
